#include <iostream>
#include <vector>
using namespace std;

bool v[305][305];

void dfs(int i, int j, int r, int c, vector<vector<char>>& grid) {
    v[i][j] = 1;
    if (i - 1 >= 0 && !v[i - 1][j] && grid[i-1][j] == '1') dfs(i - 1, j, r, c, grid);
    if (j - 1 >= 0 && !v[i][j - 1] && grid[i][j-1] == '1') dfs(i, j - 1, r, c, grid);
    if (i + 1 < r && !v[i + 1][j] && grid[i+1][j] == '1') dfs(i + 1, j, r, c, grid);
    if (j + 1 < c && !v[i][j + 1] && grid[i][j+1] == '1') dfs(i, j + 1, r, c, grid);
    return;
}

int numIslands(vector<vector<char>>& grid) {
    int ans = 0;
    for (int row = 0; row < grid.size(); row++) {
        for (int col = 0; col < grid[0].size(); col++) {
            if (grid[row][col] == '1' && !v[row][col]) {
                dfs(row, col, grid.size(), grid[0].size(), grid);
                ans++;
            }
        }
    }
    return ans;
}

int main() {
    vector<vector<char>> grid{{'1', '1', '0', '0', '0'},
                              {'1', '1', '0', '0', '0'},
                              {'0', '0', '1', '0', '0'},
                              {'0', '0', '0', '1', '1'}};

    int ans = numIslands(grid);
    cout << ans << endl;
    return 0;
}